(function () {
    // 2001：晕眩
    // 2002：减速
    // 2003：封印
    // 2004：逃脱
    // 2005：隐身

    var io = ppt.io;

    var SKILLCONFIG = {
        MAIN : {
            X : 350,
            Y : 215,
            KEYCODE : 32
        },
        PROPS : [
            {X : 185, Y : 260, KEYCODE : 49},
            {X : 195, Y : 150, KEYCODE : 50},
            {X : 280, Y : 65, KEYCODE : 51},
            {X : 390, Y : 50, KEYCODE : 52}
        ],
        SKILL : {
            X : 50,
            Y : 270,
            KEYCODE : 96
        }
    }
    
    var BtnAttack = (function (_super) {
        function BtnAttack(battleId, keyCode) {
            BtnAttack.super(this);

            this.panel = null;
            this.bubble = null;
            this.battleId = null;
            this.keyCode = null;
            
            this.init(battleId, keyCode);
        }

        Laya.class(BtnAttack, "", _super);
        var _proto = BtnAttack.prototype;
        _proto.destroy = function () {
            Laya.stage.off(Laya.Event.KEY_PRESS, this, this.onKeyPress);
            _super.prototype.destroy.call(this, true);

            this.panel = null;
            this.bubble = null;
            this.battleId = null;
            this.keyCode = null;
        }

        _proto.bindEvent = function () {
            this.panel.on(Laya.Event.CLICK, this, function () {
                this.bubble.play("click", false);
                this.bubble.offAll(Laya.Event.STOPPED);
                this.bubble.once(Laya.Event.STOPPED, this.bubble, function () {
                    this.play("wait", true);
                });

                io.emit({cmd:'move::setBubble', params : {battleId : this.battleId}});
            });

            Laya.stage.on(Laya.Event.KEY_PRESS, this, this.onKeyPress);
        }
        _proto.create = function () {
            var panel = new Laya.Image("res/game/skill/panel_main.png");
                panel.anchorX = 0.5;
                panel.anchorY = 0.5;
                panel.alpha = 0.35;
            //this.size(panel.width, panel.height);

            var bubble = utils.createSkeleton("res/game/skill/ball");
                bubble.alpha = 0.65;
                bubble.play("wait", true);

            this.panel = panel;
            this.bubble = bubble;
            this.addChildren(panel, bubble);            
        }
        
        _proto.init = function (battleId, keyCode) {
            this.battleId = battleId;
            this.keyCode = keyCode;
            this.create();
            this.bindEvent();
        }

        _proto.onKeyPress = function (e) {
            if(e.keyCode == this.keyCode){
                io.emit({cmd:'move::setBubble', params : {battleId : this.battleId}});
            }
        }
        
        return BtnAttack;
    })(Laya.Sprite);

    var BtnProps = (function (_super) {
        function BtnProps(data, keyCode) {
            BtnProps.super(this);

            this.item = null;
            this.itemId = null;
            this.num = null;
            this.totalTime = 0;
            this.interval = 100;
            this.speed = 0;
            this.speedAngle = 0;
            this.curAngle = 0;
            this.keyCode = null;

            this.init(data, keyCode);
        }
        Laya.class(BtnProps, "", _super);
        var _proto = BtnProps.prototype;
        _proto.destroy = function () {
            Laya.stage.off(Laya.Event.KEY_PRESS, this, this.onKeyPress);
            _super.prototype.destroy.call(this, true);

            this.item = null;
            this.count = null;
            this.panel = null;
            this.itemWrap = null;
            this.btnMask = null;
            this.circle = null;
            this.dot = null;
            this.time = null;
            this.cover = null;
            this.itemId = null;
            this.num = null;
            this.totalTime = null;
            this.interval = null;
            this.speed = null;
            this.speedAngle = null;
            this.curAngle = null;
            this.keyCode = null;
        }

        _proto.create = function (itemId, num) {
            var item = new Laya.Image("res/game/props/" + itemId + ".png");
                item.centerX = 0;
                item.centerY = 0;

            this.count.text = num + "";
            this.cover.on(Laya.Event.STOPPED, this, this.resetCount);
            this.circle.mask = new Laya.Sprite();

            this.item = item;
            this.itemWrap.addChild(item);
        }
        _proto.init = function (data, keyCode) {
            this.itemId = data.itemId;
            this.num = data.count;
            this.keyCode = keyCode;
            
            this.on(Laya.Event.CLICK, this, this.onClick);
            Laya.stage.on(Laya.Event.KEY_PRESS, this, this.onKeyPress);

            this.create(data.itemId, data.count);
        }
        _proto.loop = function () {
            this.totalTime -= this.speed;
            this.curAngle += this.speedAngle;

            this.time.text = (this.totalTime / 1000 | 0) + "s";

            //this.circle.mask.graphics.clear();
            this.circle.mask.graphics.drawPie(0, 0, this.circle.width / 2, 0, this.curAngle, "#000000");
            this.dot.rotation = this.curAngle + 90;

            if(this.totalTime <= 0){
                Laya.timer.clear(this, this.loop);
                this.btnMask.visible = false;
                this.circle.visible = false;
                this.dot.visible = false;
                this.time.visible = false;

                this.cover.visible = true;
                this.cover.play(0, false);
            }
        }
        _proto.resetCount = function () {
            this.cover.stop()
            this.cover.visible = false;
            this.btnMask.visible = false;
            this.circle.visible = false;
            this.dot.visible = false;
            this.time.visible = false;

            this.dot.rotation = 90;
            this.circle.mask.graphics.clear();

            this.totalTime = 0;
            this.speed = 0;
            this.speedAngle = 0;
            this.curAngle = 0;
        }
        _proto.onClick = function () {
            console.log("使用道具, ItemID:" + this.itemId);
            this.clickAni.play(0, false);
            io.emit({cmd:'item::main', params : {itemId : this.itemId}});
        }
        _proto.onKeyPress = function (e) {
            if(e.keyCode == this.keyCode){
                this.onClick();
            }
        }

        _proto.update = function (data) {
            this.resetCount();

            this.count.text = data.count;
            this.totalTime = data.cd * 1000;
            this.speed = this.totalTime / this.interval;
            this.speedAngle = 360 / this.interval;
            
            this.btnMask.visible = true;
            this.circle.visible = true;
            this.dot.visible = true;
            this.time.visible = true;
            
            Laya.timer.clear(this, this.loop);

            this.loop();
            Laya.timer.loop(this.interval, this, this.loop);
        }

        return BtnProps;
    })(ui.Game.UI.SkillPropUI);

    var BtnSkill = (function (_super) {
        function BtnSkill(skillId, keyCode) {
            BtnSkill.super(this);

            this.timer = null;
            this.iconMask = null;
            this.skillIcon = null;
            this.skillId = null;

            this.counting = false;
            this.totalTime = null;

            this.keyCode = null;

            this.init(skillId, keyCode);
        }
        Laya.class(BtnSkill, "", _super);
        var _proto = BtnSkill.prototype;
        _proto.destroy = function () {
            Laya.stage.off(Laya.Event.KEY_PRESS, this, this.onKeyPress);
            _super.prototype.destroy.call(this, true);

            this.timer = null;
            this.iconMask = null;
            this.skillIcon = null;
            this.skillId = null;
            this.counting = null;
            this.totalTime = null;
            this.keyCode = null;
        }

        _proto.loop = function () {
            this.totalTime --;
            this.timer.text = this.totalTime + "s";

            if(this.totalTime <= 0){
                Laya.timer.clear(this, this.loop);
                this.counting = false;
                this.iconMask.visible = false;
                this.timer.visible = false;
                this.timer.text = "";
            }
        }
        _proto.bindEvent = function () {
            this.icon.on(Laya.Event.CLICK, this, this.onClick);
            Laya.stage.on(Laya.Event.KEY_PRESS, this, this.onKeyPress);
        }
        _proto.create = function (skillId) {
            var icon = new Laya.Image("res/game/skill_icon/" + skillId + ".png");
                icon.anchorX = 0.5;
                icon.anchorY = 0.5;

            var timer = new Laya.Label();
                timer.bold = true;
                timer.fontSize = 20;
                timer.color = "#ffffff";
                timer.align = "center";
                timer.anchorX = 0.5;
                timer.anchorY = 0.5;
                timer.size(74, 20);
                timer.visible = false;

            var iconMask = new Laya.Image("res/game/skill/skill_mask.png");
                iconMask.anchorX = 0.5;
                iconMask.anchorY = 0.5;
                iconMask.alpha = 0.45;
                iconMask.visible = false;

            this.icon = icon;
            this.timer = timer;
            this.iconMask = iconMask;
            this.addChildren(icon, iconMask, timer);
        }
        _proto.init = function (skillId, keyCode) {
            this.keyCode = keyCode;
            this.skillId = skillId;
            this.create(skillId);
            this.bindEvent();
        }
        _proto.onClick = function () {
            if(this.counting){return;}
            console.log("使用技能，SkillId:" + this.skillId);
            io.emit({cmd:'skill::use', params : {}});
        }
        _proto.onKeyPress = function (e) {
            if(e.keyCode == this.keyCode){
                this.onClick();
            }
        }

        _proto.counter = function (time) {
            this.counting = true;
            this.timer.visible = true;
            this.iconMask.visible = true;
            this.totalTime = time;

            this.loop();
            Laya.timer.loop(1000, this, this.loop);
        }
        return BtnSkill;
    })(Laya.Sprite);


    (function (_super) {
        var ACTIONS = {
            "skill::use" : function (data) {
                this.update("skill", data);
            },
            "prop::update" : function (data) {
                this.update("prop", data);
            }
        }

        function SkillPanel(skillData, battleId) {
            SkillPanel.super(this);

            this.data = null;
            this.btnAttack = null;
            this.btnProps = {};
            this.btnSkill = null;

            this.init(skillData, battleId);
        }
        Laya.class(SkillPanel, "Game.Skill", _super);
        var _proto = SkillPanel.prototype;
        _proto.destroy = function () {
            _super.prototype.destroy.call(this, true);

            io.unregister(ACTIONS);

            this.data = null;
            this.btnAttack = null;
            this.btnProps = null;
            this.btnSkill = null;
        }

        _proto.init = function (data, battleId) {
            io.register(ACTIONS, this);
            this.setup();
            this.create(data, battleId);
        }
        _proto.setup = function () {
            this.size(450, 320);
            this.right = 0;
            this.bottom = 0;
        }
        _proto.create = function (data, battleId) {
            var props = data.props;

            var btnAttack = new BtnAttack(battleId, SKILLCONFIG.MAIN.KEYCODE);
                btnAttack.x = SKILLCONFIG.MAIN.X;
                btnAttack.y = SKILLCONFIG.MAIN.Y;

            this.addChild(btnAttack);

            for(var i = 0; i < 4; i++){
                var btnProp = new BtnProps(props[i], SKILLCONFIG.PROPS[i].KEYCODE);
                    btnProp.x = SKILLCONFIG.PROPS[i].X;
                    btnProp.y = SKILLCONFIG.PROPS[i].Y;
                    
                this.btnProps[props[i].itemId] = btnProp;
                this.addChild(btnProp);
            }

            var btnSkill = new BtnSkill(data.skill, SKILLCONFIG.SKILL.KEYCODE);
                btnSkill.x = SKILLCONFIG.SKILL.X;
                btnSkill.y = SKILLCONFIG.SKILL.Y;

            this.btnAttack = btnAttack;
            this.btnSkill = btnSkill;
            this.addChild(btnSkill);
        }
        _proto.update = function (type, data) {
            switch(type){
                case "skill":
                    this.btnSkill.counter(data.cd);
                    break;
                case "prop":
                    this.btnProps[data.propId].update(data);
                    break;
            }
        }

        _proto.resize = function (height) {
            this.bottom = 0;
        }
    })(Laya.Box);
})();

